package com.example.xmpic.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;

import com.example.common.base.BaseBindFragment;
import com.example.xmpic.R;
import com.example.xmpic.databinding.FragmentIpcPicBinding;
import com.example.xmpic.ui.adapter.LocalPictureAdapter;
import com.example.xmpic.ui.data.CameraLocalFile;
import com.example.xmpic.ui.utils.LocalFileManager;
import com.example.xmpic.ui.utils.OnLocalFileItemClick;

import java.io.File;
import java.util.List;

/**
 * @author skygge
 * @date 2019-12-30.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：摄像头本地截图
 */
public class LocalPictureFragment extends BaseBindFragment<FragmentIpcPicBinding> implements OnLocalFileItemClick {

    private String mDevSn;

    private LocalPictureAdapter mPicAdapter;
    private List<CameraLocalFile> mImageFile;

    public static LocalPictureFragment newInstance(String filePath) {
        Bundle args = new Bundle();
        args.putString("FUN_DEVICE_SN", filePath);
        LocalPictureFragment fragment = new LocalPictureFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_ipc_pic;
    }

    @Override
    protected void initialize() {
        super.initialize();
        if (getArguments()!=null){
            mDevSn = getArguments().getString("FUN_DEVICE_SN");
        }
    }

    @Override
    protected void initView() {
        super.initView();
        mPicAdapter = new LocalPictureAdapter(mContext, this);
        mDBind.picList.setLayoutManager(new LinearLayoutManager(mContext));
        mDBind.picList.setAdapter(mPicAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        new Thread(()->{
            mImageFile = LocalFileManager.getInstance().getLocalImages(mContext, mDevSn);
            if (mImageFile != null && mImageFile.size()>0){
                mHandler.post(()->{
                    mDBind.picNoData.setVisibility(View.GONE);
                    mPicAdapter.setLists(mImageFile);
                });
            }else {
                mHandler.post(()->{
                    mDBind.picNoData.setVisibility(View.VISIBLE);
                    mDBind.picList.setVisibility(View.GONE);
                });
            }
        }).start();
    }

    @Override
    public void onItemClick(CameraLocalFile image) {
        File file = new File(image.getFilepath());
        Uri photoURI;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            photoURI = FileProvider.getUriForFile(mContext, mContext.getApplicationContext().getPackageName() +".provider", file);//file即为所要共享的文件的file
        }else {
            photoURI = Uri.fromFile(file);
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//授予临时权限别忘了
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(photoURI, "image/*");
        startActivity(intent);
    }
}
